
extern plateConf cfg;

//inter character time out
const int T1_5 = 750;
//frame delay
const int T3_5 = 1750;

// --------------------
// -- COMMAND & QUE
#include "que.h"
#include "command.h"

que Q;
command C, cOut;

void processSerial(){
    int temp;
    temp = getCommand();
    while (temp) {
        wdt_reset();
        
        processQue();
        processCommand();
        temp = getCommand();
    }
}

int getCommand(){
    int retval = 0;
    while (Serial.available() > 0) {
        wdt_reset();
        
        uint8_t temp = Serial.read();
        enque(&Q,temp);
                
        if (temp == DELIMITER) {
            #ifdef DEBUG_LED
            DBG_LEDblink(RECEIVE_DEL_DBG);
            #endif
            #ifdef DEBUG_SER
            Serial.println("GOT DELIMITER");
            #endif
            return 1;
        }
        delayMicroseconds( T1_5 );
    }
    return 0;
}

void processQue(){
    C = parseQue (Q);
}

void processCommand(){
    int haveReply = 0;
    

    if (C.commandType != BROKEN_CMD){
    
    makeEmpty(&Q);
    
    switch (C.commandType){
        
        case SETTEMP_CMD:            
            #ifdef DEBUG_SER
            Serial.println("SETTEMP_CMD");
            #endif
            if ((C.value[0]==getAbsis(cfg))&&
                (C.value[1]==getOrdinat(cfg))){
                
                setTargetTemp(&cfg, C.value[2]);
                
                haveReply = 1; 
                
                cOut = makeCommand(REPTEMP_CMD,  getActTemp(cfg), getBgTemp(cfg), 0);
                sendCOut();
                
                #ifdef DEBUG_SER
                Serial.print("Target: ");
                Serial.println(getTargetTemp(cfg));
                
                Serial.print("-- Reply: ACT: ");
                Serial.print(getActTemp(cfg));
                Serial.print("   BG: ");
                Serial.println(getBgTemp(cfg));                
                #endif
            }
        break;
        case SETID_CMD:
            if (C.value[0] != 0 && C.value[1] != 0){
                EEPROM.write(ABSIS_ADD, C.value[0]);
                EEPROM.write(ORDINAT_ADD, C.value[1]);
                
                setAbsis(&cfg, C.value[0]);
                setOrdinat(&cfg, C.value[1]);
                
                #ifdef DEBUG_SER
                Serial.println("SETID_CMD");
                #endif
            }
            
            cOut = makeCommand(REPID_CMD,  getAbsis(cfg), getOrdinat(cfg), 0);
            sendCOut(); 
            
            #ifdef DEBUG_SER
            Serial.print("plateID is ( ");
            Serial.print(getAbsis(cfg));Serial.print(" , ");
            Serial.print(getOrdinat(cfg));Serial.println(" )");
            #endif
        break;
        }
    }
    
    #ifdef DEBUG_LED
    switch (C.commandType){
        case SETTEMP_CMD:       
            if (haveReply){
                DBG_LEDblink(SETTEMP_DBG_REP);
            } else {
                DBG_LEDblink(SETTEMP_DBG_NOREP);
            }
        break;
        case SETPID_CMD: 
            DBG_LEDblink(SETPID_DBG);
        break;
        case SETID_CMD:
            DBG_LEDblink(SETID_DBG);
        break;
    }
    #endif
    
}

void sendCOut(){
    if (cOut.commandType != BROKEN_CMD){
        que qOut = makeQue(cOut);
        
        delayMicroseconds(T3_5);
        
        set485Mode(TX485);       
        
        for (int i=0; i<6; ++i){
            char temp2 = deque(&qOut);
            Serial.write(temp2);
        }
        
        Serial.flush();          
        set485Mode(RX485);
        
        delayMicroseconds(T3_5);
    }
}


